package com.bringway.fm.controller;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.fm.service.AttService;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.ExcelXmlExportUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.constant.ExcelHeadConstant;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.global.service.SysAttechmentService;
import net.sf.json.JSONObject;

/*
* @author 作者 xuef
* @version 创建时间：2015年9月14日 上午10:46:55
* 类说明:业务功能-附件管理
*/
@Controller
@RequestMapping("/att")
public class AttController extends JsonController{
	
	@Autowired
	private AttService attService;
	
	@Autowired
	private SysAttechmentService attechmentService;
	
	@RequestMapping("/queryAttList")
	public ModelAndView queryAttList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("gyh", sysUserDetails.getGyh());
		params.put("username", sysUserDetails.getUsername());
		params.put("roleId", sysUserDetails.getRoleId());
		params.put("roleName", sysUserDetails.getRoleName());
		List<Map<String, Object>>  list = attService.queryAttList(params);
		int count = attService.getAttCount(params);
		result.put("data", list);	
		result.put("total", count);
		serialize(request,response,result);
		return null;
	}
	
	@RequestMapping("/deleteAtt")
	public ModelAndView deleteAtt(HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String bizId = RequestUtils.getStringParameter(request, "bizId");
		params.put("bizId", bizId);
		params.put("gyh", sysUserDetails.getGyh());
		params.put("username", sysUserDetails.getUsername());
		
		
		attService.deleteAtt(params);
		result.put("success", true);
		serialize(request,response,result);
		return null;
	}
	
	@RequestMapping("/saveAtt")
	public ModelAndView saveAtt(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String uploadOperUserId = RequestUtils.getStringParameter(request, "uploadOperUserId");
		String uploadOperUserName = RequestUtils.getStringParameter(request, "uploadOperUserName");
		String uploadDate = RequestUtils.getStringParameter(request, "uploadDate");
		String comment = RequestUtils.getStringParameter(request, "comment");
		String atts = RequestUtils.getStringParameter(request, "atts");
		String roleId = RequestUtils.getStringParameter(request, "roleId");
		String roleName = RequestUtils.getStringParameter(request, "roleName");
		String shareRoleIds = RequestUtils.getStringParameter(request, "shareRoleIds");
		String shareRoleNames = RequestUtils.getStringParameter(request, "shareRoleNames");
		params.put("uploadOperUserId", uploadOperUserId);
		params.put("uploadOperUserName", uploadOperUserName);
		params.put("uploadDate", uploadDate);
		params.put("comment", comment);
		params.put("atts", atts);
		params.put("roleId", roleId);
		params.put("roleName", roleName);
		params.put("shareRoleIds", shareRoleIds);
		params.put("shareRoleNames", shareRoleNames);
		
		
		attService.saveAtt(params);
		result.put("success", true);
		serialize(request,response,result);
		return null;
	}
	
	@RequestMapping("/listFiles")
	public void listFiles(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String gyh = RequestUtils.getStringParameter(request, "gyh");
		String username = RequestUtils.getStringParameter(request, "username");
		String fileName = RequestUtils.getStringParameter(request, "fileName");
		String comment = RequestUtils.getStringParameter(request, "comment");
		
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("gyh", gyh);
		params.put("username", username);
		params.put("currentRoleId", sysUserDetails.getRoleId());
		params.put("currentRoleName", sysUserDetails.getRoleName());
		params.put("fileName", fileName);
		params.put("comment", comment);
		Collection list = attechmentService.listAllAtt(params);
		int count = attechmentService.getAllAtt(params);
		result.put("data", list);
		result.put("total", count);
		serialize(request,response,result);
	}
	@RequestMapping("/topAtt")
	public void topAtt(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String bizId = RequestUtils.getStringParameter(request, "bizId");
		params.put("bizId", bizId);
		attService.topAtt(params);
		result.put("success", true);
		serialize(request,response,result);
	}
	
	@RequestMapping("/unTopAtt")
	public void unTopAtt(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String bizId = RequestUtils.getStringParameter(request, "bizId");
		params.put("bizId", bizId);
		attService.unTopAtt(params);
		result.put("success", true);
		serialize(request,response,result);
	}
	
	@RequestMapping("exportAttExcelMethod1")
	public void exportAttExcelMethod1(HttpServletRequest request, HttpServletResponse response) throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		JSONObject params = new JSONObject();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("gyh", sysUserDetails.getGyh());
		// 查询数据
		List<Map<String, Object>>  attList = this.attService.queryAttList(params);;
		List<String[]> dataList = ExcelHeadConstant.getAttExportSearchResultData(attList);
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName("controller直接实现");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		super.setDownLoadResponseHeader(request, response, "附件列表1.xls");
		response.setContentType("application/vnd.ms-excel");
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
	}
	
	/**
	 * 导出Excel数据
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("exportAttExcelMethod2")
	public void exportAttExcelMethod2(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		JSONObject params = new JSONObject();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("gyh", sysUserDetails.getGyh());
		// 导出页面数据
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		List<Map<String, Object>>  attList = this.attService.queryAttList(params);;
		sheetList = this.attService.exportAttData(attList);
		// 带Format的Excel导出
		ExcelOperUtil util = new ExcelOperUtil();
		// 获取template相对路径cn.com.worklog.
		InputStream stream = AttController.class.getResourceAsStream("/com/bringway/p2p/config/Att.xls");
		OutputStream os = response.getOutputStream();
		super.setDownLoadResponseHeader(request, response, "附件列表2模板导出.xls");
		util.writeToExcelByStencil(stream, sheetList, os);
	}
	
	
	/**
	 * 导出Excel数据
	 * 模板3
	 * 使用poi
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("exportAttExcelMethod3")
	public void exportAttExcelMethod3(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Map map = new HashMap();
		map.put("CUSTOMER_NAME", "李磊");
		map.put("DEPT_NAME", "王五");
		map.put("LANDLINE_NUM", "22");
		map.put("CELLPHONE_NUM", "18118096265");
		map.put("COMPANY_NAME", "中农");
		map.put("BOROUGH_NAME", "研发");
		ExcelXmlExportUtils.export("/com/bringway/fm/config/repair.xml", "报修单.xls", map, response);		
	}
	
	
	/**
	 * 导出Excel数据
	 * 模板4
	 * 使用poi 含list
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	/*@RequestMapping("exportAttExcelMethod4")
	public void exportAttExcelMethod4(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		JSONObject params = new JSONObject();
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("gyh", sysUserDetails.getGyh());
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		List<Map<String, Object>>  attList = this.attService.queryAttList(params);;
		Map map = new HashMap();
		map.put("CUSTOMER_NAME", "李磊");
		map.put("DEPT_NAME", "王五");
		map.put("LANDLINE_NUM", "22");
		map.put("CELLPHONE_NUM", "18118096265");
		map.put("COMPANY_NAME", "中农");
		map.put("BOROUGH_NAME", "研发");
		ExcelXmlExportUtils.export("/com/bringway/fm/config/repair.xml", "报修单.xls", map, response);		
	}*/
}
